﻿using Brjnk.GpxExplorer.Data.GPX.SerializationClasses;

namespace Brjnk.GpxExplorer.Data.GPX
{
    public class GpxGeocache : GeocacheInfo, IGpxObject
    {
        private cache data;

        public GpxGeocache(cache data)
        {
            this.data = data;

            this.data.name = data.name.Trim();//odstraní nový řádek na konci názvu;

            this.Logs = new MVVM.ObservableCollectionEx<GeocacheLog>();
            foreach (var l in data.logs)
            {
                this.Logs.Add(new GpxGeocacheLog(l));
            }
        }

        public GpxGeocache() : this(new cache()) { }

        public object GetUnderlayingObject()
        {
            data.logs.Clear();
            foreach (var d in Logs)
            {
                cacheLog result;
                if (d is GpxGeocacheLog)
                {
                    result = (cacheLog)(d as GpxGeocacheLog).GetUnderlayingObject();
                }
                else
                {
                    result = (cacheLog)d.ConvertTo<GpxGeocacheLog>().GetUnderlayingObject();
                }
                data.logs.Add(result);
            }

            return data;
        }

        public override string Name
        {
            get
            {
                return data.name;
            }
            set
            {
                data.name = value;
                OnPropertyChanged("Name");
            }
        }

        public override float Terrain
        {
            get
            {
                return (float)data.terrain;
            }
            set
            {
                data.terrain = value;
                data.terrainSpecified = true;
                OnPropertyChanged("Terrain");
            }
        }

        public override float Difficulty
        {
            get
            {
                return (float)data.difficulty;
            }
            set
            {
                data.difficulty = value;
                data.difficultySpecified = true;
                OnPropertyChanged("Difficulty");
            }
        }

        public override string HtmlShort
        {
            get
            {
                if (data.short_description == null) return null;
                return data.short_description.Value;
            }
            set
            {
                if (data.short_description == null) data.short_description = new cacheShort_description();
                data.short_description.Value = value;
                OnPropertyChanged("HtmlShort");
            }
        }

        public override string HtmlBody
        {
            get
            {
                if (data.long_description == null) return null;
                return data.long_description.Value;
            }
            set
            {
                if (data.long_description == null) data.long_description = new cacheLong_description();
                data.long_description.Value = value;
                OnPropertyChanged("HtmlBody");
            }
        }

        public override string Author
        {
            get
            {
                return data.placed_by;
            }
            set
            {
                if (data.owner == null) data.owner = new cacheOwner();
                data.owner.Value = value;
                OnPropertyChanged("Author");
            }
        }

        public override string Hint
        {
            get
            {
                return data.encoded_hints;
            }
            set
            {
                data.encoded_hints = value;
                OnPropertyChanged("Hint");
            }
        }

        public override GeocacheSize? Size
        {
            get
            {
                switch (data.container.ToLower())
                {
                    case "micro":
                        return GeocacheSize.Micro;
                    case "small":
                        return GeocacheSize.Small;
                    case "not chosen":
                        return GeocacheSize.NotChosen;
                    case "regular":
                        return GeocacheSize.Regular;
                    case "large":
                        return GeocacheSize.Large;
                    default:
                        return null;
                }
            }
            set
            {
                if (!value.HasValue) data.container = null;
                else if (value.Value == GeocacheSize.Micro) data.container = "Micro";
                else if (value.Value == GeocacheSize.Small) data.container = "Small";
                else if (value.Value == GeocacheSize.Regular) data.container = "Regular";
                else if (value.Value == GeocacheSize.Large) data.container = "Large";
                else data.container = null;
                OnPropertyChanged("Size");
            }
        }

        public override GeocacheType? Type
        {
            get
            {
                switch (data.type.ToLower())
                {
                    case "traditional cache":
                        return GeocacheType.Traditional;
                    case "multi-cache":
                        return GeocacheType.Multi;
                    case "unknown cache":
                        return GeocacheType.Puzzle;
                    case "wherigo cache":
                        return GeocacheType.Wherigo;
                    case "event":
                        return GeocacheType.Event;
                    case "earth cache":
                        return GeocacheType.EarthCache;
                    case "webcam cache":
                        return GeocacheType.Webcam;
                    default:
                        return null;
                }
            }
            set
            {
                if (!value.HasValue) data.type = null;
                else
                    switch (value.Value)
                    {
                        case GeocacheType.Unknown:
                            data.type = null;
                            break;

                        case GeocacheType.Traditional:
                            data.type = "Traditional Cache";
                            break;

                        case GeocacheType.Multi:
                            data.type = "Multi-cache";
                            break;

                        case GeocacheType.Puzzle:
                            data.type = "Unknown Cache";
                            break;

                        case GeocacheType.Wherigo:
                            data.type = "Wherigo Cache";
                            break;

                        case GeocacheType.Event:
                            data.type = "Event";
                            break;

                        case GeocacheType.MegaEvent:
                            data.type = "Mega Event";
                            break;

                        case GeocacheType.EarthCache:
                            data.type = "Earth Cache";
                            break;

                        case GeocacheType.Webcam:
                            data.type = "Webcam cache";
                            break;

                        default:
                            data.type = null;
                            break;
                    }
                OnPropertyChanged("Type");
            }
        }
    }
}